package com.project2.controller;

import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.po.MarketAd;
import com.project2.bean.vo.AdminPageInfo;
import com.project2.service.AdminAdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 广告模块
 * @author Frank Zhu
 */
@RequestMapping("admin/ad")
@RestController
public class AdminAdController {

    @Autowired
    AdminAdService adminAdService;

    /**
     * 后台广告信息的回显
     * @param pageInfo 基本的页面信息
     * @param name 广告名，模糊查询
     * @param content 广告内容模糊查询
     * @return
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:ad:list")
    @RequestMapping("list")
    public ResultVo getAdList(PageBo pageInfo, String name, String content){
        CommonData allAds = adminAdService.getAllAds(pageInfo, name, content);

        return ResultVo.ok(allAds);
    }

    /**
     * 增加广告信息，需要有url返回
     * @param newAd
     * @return
     */
    @RequiresPermissions("admin:ad:create")
    @RequestMapping("create")
    public ResultVo createAd(@RequestBody MarketAd newAd){
        Boolean done = adminAdService.createAd(newAd);
        if(!done){
            ResultVo.wrong("新建广告失败。");
        }
        return ResultVo.ok();
    }

    @RequiresPermissions("admin:ad:update")
    @RequestMapping("update")
    public ResultVo updateAd(@RequestBody MarketAd ad){

        MarketAd marketAd = adminAdService.updateAd(ad);
        if(marketAd == null){
            return ResultVo.wrong("更新失败，请稍后再试。");
        }
        return ResultVo.ok(marketAd);
    }

    /**
     * 删除广告信息
     * @param ad
     * @return
     */
    @RequiresPermissions("admin:ad:delete")
    @RequestMapping("delete")
    public ResultVo deleteAd(@RequestBody MarketAd ad){
        Boolean done = adminAdService.deleteAd(ad.getId());
        if(!done){
            return ResultVo.wrong("删除失败，请稍后再试。");
        }
        return ResultVo.ok();
    }
}
